<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/5/29
 * Time: 14:13
 * Mail:87658989@qq.com
 */

namespace app\adminapi\validate;

use app\base\validate\BaseValidate;

class CarlifeValidate extends BaseValidate
{
    protected $rule = [
        'fields'    => 'checkField',
        'where_json'=> 'checkJson',
        'page'      => 'number',
        'pagenum'   => 'number',
        'counts'    => 'number',

        'id' => 'require|checkIDs',
    ];

    protected $message = [
        'where_json.checkJson'=>'必须为json字符串'
    ];

    protected $default = [
        'page'   =>1,
        'pagenum'=>10,
        'fields' =>'*',
        'counts' =>9999999
    ];

    public $scene = [
        'report'=>['fields','where_json','page','pagenum'],
        'add_buy' => [
            'company_id',
            'user_name',
            'car_brand_id',
            'car_series_id',
            'car_model_id',
            'people',
            'money',
            'buyto',
            'whybuy',
            'state',
            'car_id',
        ],
        'edit_buy' => [
            'car_audit_id',
            'company_id',
            'user_name',
            'car_brand_id',
            'car_series_id',
            'car_model_id',
            'people',
            'money',
            'buyto',
            'whybuy',
            'state',
            'car_id',
        ],
        'edit_sell' => [
            'car_auction_id',
            'people',
            'true_money',
            'want_money',
            'why',
            'state',
            'car_id',
        ],
        'edit_scrap' => [
            'car_scrap_id',
            'why',
            'state',
            'car_id',
        ],
        'delete_buy' => [ 'id' ],
        'delete_sell' => [ 'car_auction_ids' ],
        'delete_scrap' => [ 'car_scrap_ids' ],
    ];

    public function checkField($value){
        if($value=='*') return true;
        return true;
    }

    public function checkJson($value){
        if($value == null) return true;
        if(is_array(json_decode($value,true))) return true;
        return false;
    }
}